package com.supercube.sport.yoga.service.impl;

import com.supercube.common.utils.ShiroUtils;
import com.supercube.common.utils.TimeUtils;
import com.supercube.sport.yoga.bo.MemberGoodsBo;
import com.supercube.sport.yoga.dao.GoodsStockMapper;
import com.supercube.sport.yoga.dao.MemberGoodsMapper;
import com.supercube.sport.yoga.dao.StockRecordMapper;
import com.supercube.sport.yoga.domain.GoodsStock;
import com.supercube.sport.yoga.domain.MemberGoods;
import com.supercube.sport.yoga.domain.StockRecord;
import com.supercube.sport.yoga.service.GoodsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * Description：
 * Created By guowen
 */
@Service
public class GoodsStockServiceImpl implements GoodsStockService {
    @Autowired
    private GoodsStockMapper goodsStockMapper;
    @Autowired
    private MemberGoodsMapper memberGoodsMapper;
    @Autowired
    private StockRecordMapper stockRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGoods (GoodsStock dto){
        String currentTime = TimeUtils.getCurrentTime();
        String userId = ShiroUtils.getUserId().toString();
        dto.setId(UUID.randomUUID().toString());
        dto.setCreatedDate(TimeUtils.getCurrentTime());
        dto.setCreatedBy(userId);
        dto.setUpdateBy(userId);
        dto.setUpdateDate(currentTime);
        dto.setStatus("4");//赠送
        dto.setProfit(Integer.valueOf(dto.getSalePrice())-Integer.valueOf(dto.getBuyingPrice())+"");
        dto.setInDate(currentTime);
        goodsStockMapper.insert(dto);

        StockRecord record = new StockRecord();
        record.setId(UUID.randomUUID().toString());
        record.setGoodsId(dto.getId());
        record.setInOutDate(currentTime);
        record.setType("1");//入库
        record.setAmount(dto.getAmount());
        record.setCreatedDate(currentTime);
        record.setCreatedBy(userId);
        record.setUpdateBy(userId);
        record.setUpdateDate(currentTime);
        stockRecordMapper.insert(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editGoods (GoodsStock dto){
        String currentTime = TimeUtils.getCurrentTime();
        String userId = ShiroUtils.getUserId().toString();
        dto.setUpdateBy(userId);
        dto.setUpdateDate(currentTime);
        dto.setStatus("4");//赠送
        dto.setProfit(Integer.valueOf(dto.getSalePrice())-Integer.valueOf(dto.getBuyingPrice())+"");
        dto.setInDate(currentTime);
        GoodsStock dto1 = goodsStockMapper.selectByPrimaryKey(dto.getId());
        if(dto.getChangeAmount() > 0){
            dto.setAmount(Integer.valueOf(dto1.getSalePrice()) + dto.getChangeAmount()+"");
        }else{
            dto.setAmount(Integer.valueOf(dto1.getSalePrice()) - dto.getChangeAmount()+"");
        }
        goodsStockMapper.updateByPrimaryKeySelective(dto);

        StockRecord record = new StockRecord();
        record.setId(UUID.randomUUID().toString());
        record.setGoodsId(dto.getId());
        record.setInOutDate(currentTime);

        if(dto.getChangeAmount() > 0){
            record.setType("1");//入库
            record.setAmount(dto.getChangeAmount()+"");
        }else{
            record.setType("2");//出库
            record.setAmount(dto.getChangeAmount()+"");
        }

        record.setCreatedDate(currentTime);
        record.setCreatedBy(userId);
        record.setUpdateBy(userId);
        record.setUpdateDate(currentTime);
        stockRecordMapper.insert(record);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void memberByGoods (MemberGoodsBo bo){
        GoodsStock dto = goodsStockMapper.selectByPrimaryKey(bo.getGoodsId());
        String currentTime = TimeUtils.getCurrentTime();
        String userId = ShiroUtils.getUserId().toString();
        dto.setUpdateBy(userId);
        dto.setUpdateDate(currentTime);
        //总库存-购买数量
        dto.setAmount(Integer.parseInt(dto.getAmount())-Integer.parseInt(bo.getBuyNum())+"");
        goodsStockMapper.updateByPrimaryKeySelective(dto);
        MemberGoods memberGoods = new MemberGoods();
        memberGoods.setId(UUID.randomUUID().toString());
        memberGoods.setGoodsId(bo.getGoodsId());
        memberGoods.setBuyNum(bo.getBuyNum());
        memberGoods.setBuyingPrice(dto.getBuyingPrice());
        memberGoods.setPrice(dto.getPrice());
        memberGoods.setSalePrice(dto.getSalePrice());
        memberGoods.setProfit(dto.getProfit());
        memberGoods.setBuyType("2");//购买类型 1:购买，2：赠送
        memberGoods.setMemberId(bo.getMemberId());
        memberGoods.setMemberName(bo.getMemberName());
        memberGoods.setCardType(bo.getCardType());//卡项
        memberGoods.setSaleDate(currentTime);//销售时间
        memberGoods.setOrderDate(currentTime);//订单时间
        memberGoods.setZb(bo.getZb());
        memberGoods.setCreatedDate(currentTime);
        memberGoods.setCreatedBy(userId);
        memberGoods.setUpdateBy(userId);
        memberGoods.setUpdateDate(currentTime);
        memberGoodsMapper.insert(memberGoods);

        StockRecord record = new StockRecord();
        record.setId(UUID.randomUUID().toString());
        record.setGoodsId(dto.getId());
        record.setInOutDate(currentTime);
        record.setType("2");//出库
        record.setAmount(bo.getBuyNum());
        record.setCreatedDate(currentTime);
        record.setCreatedBy(userId);
        record.setUpdateBy(userId);
        record.setUpdateDate(currentTime);
        stockRecordMapper.insert(record);
    }


}
